﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGinnBPM.MessageBus.Windsor;
using Castle.Windsor;
using NLog;
using System.Configuration;

namespace NGinnBPM.HostConfig.Install
{
    class PluginInstaller : IPlugin
    {
        private Logger log = LogManager.GetCurrentClassLogger();

        private NGinnHostConfigurator _cfgBuilder;

        public PluginInstaller()
        {
            log.Info("NGinn BPM plugin installer created");
        }

        public void OnFinishConfiguration(IWindsorContainer wc)
        {
            log.Info("Finishing NGinn BPM configuration");
            _cfgBuilder.FinishConfiguration();
        }

        public void Register(IWindsorContainer wc)
        {
            string bd = AppDomain.CurrentDomain.BaseDirectory;
            string cfgFile = ConfigurationManager.AppSettings["NGinnBPM.ConfigFile"];
            log.Info("Configuring NGinn BPM in {0}", bd);
            _cfgBuilder = NGinnHostConfigurator.BeginConfig(wc);
            _cfgBuilder.SetBaseDirectory(bd);
            if (!string.IsNullOrEmpty(cfgFile))
            {
                log.Info("Will use config file: {0}", cfgFile);
                _cfgBuilder.SetConfigFile(cfgFile);
            }
            else
            {
                log.Info("Will use app-config settings");
                _cfgBuilder.SetConfigProvider(new NGinnBPM.Lib.Util.AppConfigProvider());
            }
        }
    }
}
